package TestSparkConversionOperator;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;

import java.util.Arrays;
import java.util.List;

public class SparkIntersection {
    /**
     * 获取两个RDD的相同元素，并返回一个新的RDD。
     */
    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf().setAppName("Distinct").setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(sparkConf);

        JavaRDD<Integer> rdd1 = sc.parallelize(Arrays.asList(1, 1, 2, 2, 3, 3), 2);
        JavaRDD<Integer> rdd2 = sc.parallelize(Arrays.asList(1, 5, 7, 8), 2);

        JavaRDD<Integer> intersection = rdd1.intersection(rdd2);
        List<Integer> res = intersection.collect();

        for (Integer i : res){
            System.out.println(i);
        }


    }
}
